package com.wnn.demo.utils;


import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Data
@Component
public class PageUtil<T> {


//	页码
	private Integer pageNum;

//  数量
	private Integer pageSize;

//	总页数
	private Integer pageCount;

//  对象集合
	private List<T> list;

	public void startPage(List list, Integer pageNum,
								 Integer pageSize) {

// 记录总数
		Integer count = list.size();
// 页数
		Integer pageCount = 0;
		if (count % pageSize == 0) {
			pageCount = count / pageSize;
		} else {
			pageCount = count / pageSize + 1;
		}
// 开始索引
		int fromIndex = 0;
// 结束索引
		int toIndex = 0;

		if (pageNum < pageCount) {
			fromIndex = (pageNum - 1) * pageSize;
			toIndex = fromIndex + pageSize;
		} else {
			fromIndex = (pageCount - 1) * pageSize;
			toIndex = count;
		}
		List pageList = new ArrayList();
		if (fromIndex > list.size() || toIndex > list.size()) {
		} else {
			pageList = list.subList(fromIndex, toIndex);
		}

		this.list = pageList;
		this.pageCount = pageCount;
		this.pageNum = pageNum;
		this.pageSize = pageSize;
	}
	/*public static List startPage(List list, Integer pageNum,
								 Integer pageSize) {
		if (list == null) {
			return null;
		}
		if (list.size() == 0) {
			return null;
		}
// 记录总数
		Integer count = list.size();
// 页数
		Integer pageCount = 0;
		if (count % pageSize == 0) {
			pageCount = count / pageSize;
		} else {
			pageCount = count / pageSize + 1;
		}
// 开始索引
		int fromIndex = 0;
// 结束索引
		int toIndex = 0;

		if (pageNum != pageCount) {
			fromIndex = (pageNum - 1) * pageSize;
			toIndex = fromIndex + pageSize;
		} else {
			fromIndex = (pageNum - 1) * pageSize;
			toIndex = count;
		}
		List pageList = new ArrayList();
		if (fromIndex > list.size() || toIndex > list.size()) {
		} else {
			pageList = list.subList(fromIndex, toIndex);
		}

		return pageList;
	}*/
}

